{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "连接成功\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "from xtquant.xttrader import XtQuantTrader\n",
    "\n",
    "# miniQMT安装路径\n",
    "mini_qmt_path = r'D:\\国金QMT交易端模拟\\userdata_mini'\n",
    "# 创建session_id\n",
    "session_id = int(random.randint(100000, 999999))\n",
    "# 创建交易对象\n",
    "xt_trader = XtQuantTrader(mini_qmt_path, session_id)\n",
    "# 启动交易对象\n",
    "xt_trader.start()\n",
    "# 连接客户端\n",
    "connect_result = xt_trader.connect()\n",
    "\n",
    "if connect_result == 0:\n",
    "    print('连接成功')\n",
    "\n",
    "from xtquant.xttype import StockAccount\n",
    "# QMT账号\n",
    "account_id = '10503139'\n",
    "# 创建账号对象\n",
    "acc = StockAccount(account_id)\n",
    "# 订阅账号\n",
    "res = xt_trader.subscribe(acc)\n",
    "# print(account)\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "query positions:\n",
      "positions: 67\n",
      "110061.SH 90\n",
      "511880.SH 400\n",
      "513180.SH 1200\n",
      "600000.SH 200\n",
      "600001.SH 200\n",
      "600002.SH 300\n",
      "600003.SH 100\n",
      "600025.SH 500\n",
      "600031.SH 2000\n",
      "600039.SH 500\n",
      "600048.SH 1000\n",
      "600061.SH 500\n",
      "600104.SH 4500\n",
      "600252.SH 500\n",
      "600276.SH 1600\n",
      "600426.SH 500\n",
      "600519.SH 1200\n",
      "600547.SH 500\n",
      "600556.SH 3000\n",
      "600584.SH 500\n",
      "600690.SH 3300\n",
      "600812.SH 500\n",
      "600905.SH 4000\n",
      "600918.SH 500\n",
      "600958.SH 600\n",
      "600971.SH 100\n",
      "600979.SH 2000\n",
      "601001.SH 500\n",
      "601136.SH 2000\n",
      "601166.SH 4000\n",
      "601225.SH 600\n",
      "601375.SH 500\n",
      "601399.SH 500\n",
      "601615.SH 500\n",
      "601633.SH 1500\n",
      "601696.SH 1800\n",
      "601857.SH 8000\n",
      "601860.SH 500\n",
      "601878.SH 500\n",
      "601888.SH 400\n",
      "603312.SH 1500\n",
      "603799.SH 1900\n",
      "688111.SH 1900\n",
      "000001.SZ 100\n",
      "000060.SZ 1500\n",
      "000426.SZ 500\n",
      "000429.SZ 200\n",
      "000605.SZ 1000\n",
      "000632.SZ 1000\n",
      "000783.SZ 400\n",
      "000878.SZ 100\n",
      "000975.SZ 1900\n",
      "001229.SZ 300\n",
      "001965.SZ 1500\n",
      "002084.SZ 600\n",
      "002252.SZ 400\n",
      "002340.SZ 1500\n",
      "002716.SZ 600\n",
      "002747.SZ 3300\n",
      "002809.SZ 600\n",
      "002853.SZ 500\n",
      "003017.SZ 600\n",
      "300462.SZ 1500\n",
      "300865.SZ 600\n",
      "300947.SZ 1000\n",
      "301063.SZ 400\n",
      "301197.SZ 1900\n"
     ]
    }
   ],
   "source": [
    "# 查询当日所有的持仓\n",
    "print(\"query positions:\")\n",
    "positions = xt_trader.query_stock_positions(acc)\n",
    "print(\"positions:\", len(positions))\n",
    "if len(positions) != 0:\n",
    "    for i in range(len(positions)):\n",
    "        print(f\"{positions[i].stock_code} {positions[i].volume}\")\n",
    "else:\n",
    "    print(f\"{positions[0].stock_code} {positions[0].volume}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python39",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
